/*
 * Copyright 2012 Honestyworks Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package jp.honestyworks.pbcache;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.apphosting.api.ApiProxy.ApiConfig;
import com.google.apphosting.api.ApiProxy.ApiProxyException;
import com.google.apphosting.api.ApiProxy.Delegate;
import com.google.apphosting.api.ApiProxy.Environment;
import com.google.apphosting.api.ApiProxy.LogRecord;

/**
 * ApiProxy hook.
 * 
 * @author miztaka
 *
 * @param <E>
 */
public class ApiProxyHook<E extends Environment> implements Delegate<E> {
    
    private static final Log logger = LogFactory.getLog(ApiProxyHook.class);

    private Delegate<E> baseDelegate;
    private Map<String, Delegate<E>> hooks = new HashMap<String, Delegate<E>>();
    
    public ApiProxyHook(Delegate<E> base) {
            this.baseDelegate = base;
    }
    
    public Delegate<E> getBaseDelegate() {
            return baseDelegate;
    }

    public Map<String, Delegate<E>> getHooks() {
            return hooks;
    }
    
    public Future<byte[]> makeAsyncCall(E environment, String packageName, String methodName,
            byte[] request, ApiConfig config) {
        
        logger.debug("makeAsyncCall: " + packageName + "#" + methodName);
        
        Delegate<E> hook = this.hooks.get(packageName);
        if(hook != null) {
            return hook.makeAsyncCall(environment, packageName, methodName, request, config);
        } else {
            return this.baseDelegate.makeAsyncCall(environment, packageName, methodName, request, config);
        }
    }

    public byte[] makeSyncCall(E environment, String packageName, String methodName, byte[] request)
            throws ApiProxyException {
        
        logger.debug("makeSyncCall: " + packageName + "#" + methodName);
        
        Delegate<E> hook = this.hooks.get(packageName);
        if(hook != null) {
            return hook.makeSyncCall(environment, packageName, methodName, request);
        } else {
            return this.baseDelegate.makeSyncCall(environment, packageName, methodName, request);
        }
    }

    public void flushLogs(E arg0) {
        baseDelegate.flushLogs(arg0);
    }

    public List<Thread> getRequestThreads(E arg0) {
        return baseDelegate.getRequestThreads(arg0);
    }

    public void log(E arg0, LogRecord arg1) {
        baseDelegate.log(arg0, arg1);
    }

}
